import 'dart:convert';
import 'package:hinge/common/index.dart';

/// 管理用户与视频的交互状态（点赞、收藏等）
class VideoInteractionService {
  // 存储键名
  static const String _videoLikesKey = 'video_likes';
  static const String _videoCollectsKey = 'video_collects';

  // 获取所有点赞记录
  static Map<String, List<int>> _getAllLikes() {
    final String? jsonStr = SpUtil.getString(_videoLikesKey);
    if (jsonStr == null || jsonStr.isEmpty) return {};

    try {
      final Map<String, dynamic> data = json.decode(jsonStr);
      Map<String, List<int>> result = {};

      data.forEach((key, value) {
        if (value is List) {
          result[key] = List<int>.from(value);
        }
      });

      return result;
    } catch (e) {
      return {};
    }
  }

  // 获取所有收藏记录
  static Map<String, List<int>> _getAllCollects() {
    final String? jsonStr = SpUtil.getString(_videoCollectsKey);
    if (jsonStr == null || jsonStr.isEmpty) return {};

    try {
      final Map<String, dynamic> data = json.decode(jsonStr);
      Map<String, List<int>> result = {};

      data.forEach((key, value) {
        if (value is List) {
          result[key] = List<int>.from(value);
        }
      });

      return result;
    } catch (e) {
      return {};
    }
  }

  // 检查用户是否点赞了某视频
  static bool isVideoLiked(int userId, int videoId) {
    final likes = _getAllLikes();
    final userKey = userId.toString();
    return likes.containsKey(userKey) && likes[userKey]!.contains(videoId);
  }

  // 检查用户是否收藏了某视频
  static bool isVideoCollected(int userId, int videoId) {
    final collects = _getAllCollects();
    final userKey = userId.toString();
    return collects.containsKey(userKey) &&
        collects[userKey]!.contains(videoId);
  }

  // 添加点赞
  static Future<void> addLike(int userId, int videoId) async {
    final likes = _getAllLikes();
    final userKey = userId.toString();

    if (!likes.containsKey(userKey)) {
      likes[userKey] = [];
    }

    if (!likes[userKey]!.contains(videoId)) {
      likes[userKey]!.add(videoId);
      await SpUtil.setString(_videoLikesKey, json.encode(likes));
    }
  }

  // 移除点赞
  static Future<void> removeLike(int userId, int videoId) async {
    final likes = _getAllLikes();
    final userKey = userId.toString();

    if (likes.containsKey(userKey)) {
      likes[userKey]!.remove(videoId);
      await SpUtil.setString(_videoLikesKey, json.encode(likes));
    }
  }

  // 添加收藏
  static Future<void> addCollect(int userId, int videoId) async {
    final collects = _getAllCollects();
    final userKey = userId.toString();

    if (!collects.containsKey(userKey)) {
      collects[userKey] = [];
    }

    if (!collects[userKey]!.contains(videoId)) {
      collects[userKey]!.add(videoId);
      await SpUtil.setString(_videoCollectsKey, json.encode(collects));
    }
  }

  // 移除收藏
  static Future<void> removeCollect(int userId, int videoId) async {
    final collects = _getAllCollects();
    final userKey = userId.toString();

    if (collects.containsKey(userKey)) {
      collects[userKey]!.remove(videoId);
      await SpUtil.setString(_videoCollectsKey, json.encode(collects));
    }
  }

  // 获取用户所有点赞的视频ID
  static List<int> getUserLikedVideos(int userId) {
    final likes = _getAllLikes();
    final userKey = userId.toString();
    return likes[userKey] ?? [];
  }

  // 获取用户所有收藏的视频ID
  static List<int> getUserCollectedVideos(int userId) {
    final collects = _getAllCollects();
    final userKey = userId.toString();
    return collects[userKey] ?? [];
  }
}
